﻿package mortal.game.view.wizard
{
    import com.gengine.core.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import fl.controls.*;
    import flash.geom.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;

    public class GTabarNew extends GSprite
    {
        private var _tab:GTabBar;
        private var _leftLine:ScaleBitmap;
        private var _rightLine:ScaleBitmap;
        private var _bg:ScaleBitmap;
        private var _isHasFilter:Boolean = false;
        private var _bgArr:Array;
        private var _numArr:Array;

        public function GTabarNew()
        {
            this._bgArr = [];
            this._numArr = [];
            return;
        }// end function

        public function getTabRectByLabel(param1:String) : Rectangle
        {
            if (this._tab == null)
            {
                return new Rectangle();
            }
            var _loc_2:* = this._tab.getTabRectByLabel(param1);
            _loc_2.x = _loc_2.x + 8;
            return _loc_2;
        }// end function

        public function getIndexByLabel(param1:String) : int
        {
            var _loc_4:Object = null;
            var _loc_2:* = this._tab.dataProvider;
            if (_loc_2 == null)
            {
                return -1;
            }
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_3];
                if (_loc_4.label == param1)
                {
                    return _loc_3;
                }
                _loc_3++;
            }
            return -1;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._tab = UIFactory.gTabBar(6, 0, null, 72, 22, this, this.changeHandler, "TabButtonNew");
            this._bg = ResourceConst.getScaleBitmap("SelsetBg");
            this._leftLine = GlobalClass.getScaleBitmap(ImagesConst.LeftWinLine, new Rectangle(6, 0, 1, 1));
            this.addChild(this._leftLine);
            this._rightLine = GlobalClass.getScaleBitmap(ImagesConst.rightWinLine, new Rectangle(2, 0, 1, 1));
            this.addChild(this._rightLine);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.clearBarFilter();
            this._leftLine.dispose(param1);
            this._leftLine = null;
            this._rightLine.dispose(param1);
            this._rightLine = null;
            this._tab.dispose(param1);
            this._tab = null;
            this._bg.dispose(param1);
            this._bg = null;
            this.clearAllNum();
            this._isHasFilter = false;
            super.disposeImpl(param1);
            return;
        }// end function

        override public function setSize(param1:Number, param2:Number) : void
        {
            _width = param1;
            _height = param2;
            return;
        }// end function

        public function set dataProvider(param1:Array) : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            if (param1)
            {
                this._tab.dataProvider = param1;
                while (_loc_3 < param1.length)
                {
                    
                    _loc_2 = param1[_loc_3];
                    if (_loc_2.hasOwnProperty("num"))
                    {
                        this.setLeftNum(_loc_3, _loc_2.num);
                    }
                    _loc_3++;
                }
                this.drawNow();
            }
            return;
        }// end function

        public function get dataProvider() : Array
        {
            return this._tab.dataProvider;
        }// end function

        public function set buttonWidth(param1:Number) : void
        {
            this._tab.buttonWidth = param1;
            return;
        }// end function

        public function set buttonHeight(param1:Number) : void
        {
            this._tab.buttonHeight = param1;
            return;
        }// end function

        public function set buttonStyleName(param1:String) : void
        {
            this._tab.buttonStyleName = param1;
            return;
        }// end function

        public function set horizontalGap(param1:int) : void
        {
            this._tab.horizontalGap = param1;
            return;
        }// end function

        public function set buttonFilters(param1:Array) : void
        {
            this._tab.buttonFilters = param1;
            return;
        }// end function

        public function set selectedIndex(param1:int) : void
        {
            this._tab.selectedIndex = param1;
            this.updateLinePos();
            return;
        }// end function

        public function get selectedIndex() : int
        {
            return this._tab.selectedIndex;
        }// end function

        public function get tab() : GTabBar
        {
            return this._tab;
        }// end function

        public function drawNow() : void
        {
            this.updateLinePos();
            this._tab.drawNow();
            return;
        }// end function

        private function updateLinePos() : void
        {
            if (this._leftLine && this._tab && this._rightLine && this._bg)
            {
                this._leftLine.y = this._tab.buttonHeight - 4;
                this._leftLine.width = 10 + this._tab.selectedIndex * (this._tab.horizontalGap + this._tab.buttonWidth);
                this._rightLine.y = this._tab.buttonHeight - 4;
                this._rightLine.x = 6 + this._tab.selectedIndex * (this._tab.horizontalGap + this._tab.buttonWidth) + this._tab.buttonWidth;
                this._rightLine.width = _width - this._rightLine.x + 2;
                this._bg.y = this._tab.buttonHeight - 2;
                this._bg.width = _width;
            }
            return;
        }// end function

        private function changeHandler(event:MuiEvent = null) : void
        {
            this.updateLinePos();
            dispatchEvent(new MuiEvent(MuiEvent.GTABBAR_SELECTED_CHANGE, this._tab.selectedIndex));
            return;
        }// end function

        public function showFilterByIndex(param1:int) : void
        {
            var _loc_2:Button = null;
            if (this._tab)
            {
                _loc_2 = this._tab.getButtonAt(param1);
            }
            if (_loc_2)
            {
                EffectManager.glowFilterUnReg(_loc_2);
                EffectManager.glowFilterReg(_loc_2);
                this._isHasFilter = true;
            }
            return;
        }// end function

        public function clearFilterByIndex(param1:int) : void
        {
            var _loc_2:Button = null;
            if (this._tab)
            {
                _loc_2 = this._tab.getButtonAt(param1);
            }
            if (_loc_2)
            {
                EffectManager.glowFilterUnReg(_loc_2);
            }
            return;
        }// end function

        public function clearBarFilter() : void
        {
            var _loc_1:Button = null;
            var _loc_2:int = 0;
            if (this._isHasFilter)
            {
                while (_loc_2 < this._tab.dataProvider.length)
                {
                    
                    _loc_1 = this._tab.getButtonAt(_loc_2);
                    if (_loc_1)
                    {
                        EffectManager.glowFilterUnReg(_loc_1);
                    }
                    _loc_2++;
                }
                this._isHasFilter = false;
            }
            return;
        }// end function

        private function clearAllNum() : void
        {
            var _loc_1:IDispose = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._bgArr.length)
            {
                
                _loc_1 = this._bgArr[_loc_2] as IDispose;
                if (_loc_1)
                {
                    _loc_1.dispose(true);
                }
                _loc_1 = this._numArr[_loc_2] as IDispose;
                if (_loc_1)
                {
                    _loc_1.dispose(true);
                }
                _loc_2++;
            }
            this._bgArr.length = 0;
            this._numArr.length = 0;
            return;
        }// end function

        public function setLeftNum(param1:int, param2:int) : void
        {
            var _loc_4:GTextFiled = null;
            var _loc_3:* = this._bgArr[param1];
            _loc_4 = this._numArr[param1];
            if (param2 > 0)
            {
                if (_loc_3 == null)
                {
                    _loc_3 = GlobalClass.getBitmap(ImagesConst.NumberBg);
                    this.addChild(_loc_3);
                    this._bgArr[param1] = _loc_3;
                }
                if (_loc_4 == null)
                {
                    _loc_4 = UIFactory.gTextField("", 22, 1, 20, 20, null, GlobalStyle.textFormatBai.center());
                    _loc_4.mouseEnabled = false;
                    this.addChild(_loc_4);
                    this._numArr[param1] = _loc_4;
                }
                _loc_3.x = (this._tab.buttonWidth + this._tab.horizontalGap) * (param1 + 1) - 15;
                _loc_3.y = 1;
                _loc_4.x = (this._tab.buttonWidth + this._tab.horizontalGap) * (param1 + 1) - 13;
                _loc_4.y = 2;
                _loc_4.text = param2.toString();
            }
            else
            {
                if (_loc_3)
                {
                    _loc_3.dispose();
                    this._bgArr[param1] = null;
                }
                if (_loc_4)
                {
                    _loc_4.dispose();
                    this._numArr[param1] = null;
                }
            }
            return;
        }// end function

    }
}
